package com.recycler.coverflow.wcdb;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.TextView;

import com.recycler.coverflow.R;

import java.io.File;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.List;

public class WcdbTextActivity extends AppCompatActivity {
    private static final String TAG = "zyj_db_a";
    private TextView mTv;
    StringBuilder builder = new StringBuilder();
    private ErcDaoManager mManager;
    int i;

    private String[] SEARCH_TAG = {
            "123",
            "小酒窝",
            "你是",
            "如果爱",
            "那一年的雪花飘落梅花开枝头",
            "十年之前"

    };

    private void startTest(boolean isMatch){
        for (String tag:SEARCH_TAG) {
            testSearch(tag,isMatch);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wcdb_text);
        mTv = findViewById(R.id.tv);
        mManager = new ErcDaoManager(WcdbTextActivity.this);


        new Thread(){
            @Override
            public void run() {
                super.run();


                long c1 = System.currentTimeMillis();

                int count = mManager.getCount();
                File file = new File("/data/data/com.recycler.coverflow/databases/1.db");
                long length = file.length();
                setTextLine("==============>数据量："+count+" 大小："+ Formatter.formatFileSize(WcdbTextActivity.this, length) );

                Log.w(TAG,"createFt3IfNotExit >>> "+(System.currentTimeMillis() - c1));

                long l = System.currentTimeMillis();

                startTest(false);

                long l2 = System.currentTimeMillis() - l;
                setTextLine("==============>like 查询总耗时："+l2+"ms,"+i);

                //全文搜索
                i =0;
                long m = System.currentTimeMillis();
                startTest(true);


                long m2 = System.currentTimeMillis() - m;
                setTextLine("==============>match 查询总耗时："+m2+"ms,"+i);

            }
        }.start();
    }

    private void testSearch(String tag, boolean isMatch) {
        i++;

        setTextLine(">>>>"+"开始搜索："+tag);
        List<String> likeTagData = isMatch?mManager.getMatchTagData(tag):
                mManager.getLikeTagData(tag);
        setTextLine("<<<<"+"结束搜索："+tag);
        if(likeTagData != null){
            for (String s:likeTagData) {
                Log.i(TAG, ">>>>"+tag+"_"+isMatch+"_"+s);
            }
        }
    }

    private void setTextLine(final String line){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat timeFormat = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                builder.append(timeFormat.format(System.currentTimeMillis())+"["+line+"]").append("\n");
                mTv.setText(builder.toString());
            }
        });
    };
}
